//
// Created by changlei on 2022/4/29.
//

import Foundation
import UIKit


class UIFontSizeMenu: UIMenu {
    var onHideToolbar: (() -> Void)?

    override var children: [UIMenuElement] {
        [
            UIMenu(options: .displayInline, children: [
                UIAction(title: "") { action in
                },
                UIAction(title: "显示阅读器", image: UIImage(systemName: "doc.plaintext"), attributes: .disabled) { action in
                },
            ]),
            UIMenu(options: .displayInline, children: [
                UIAction(title: "翻译为英语", image: UIImage(systemName: "character.bubble")) { action in
                },
                UIAction(title: "报告翻译错误", image: UIImage(systemName: "exclamationmark.bubble"), attributes: .disabled) { action in
                },
            ]),
            UIMenu(options: .displayInline, children: [
                UIAction(title: "隐藏工具栏", image: UIImage(systemName: "arrow.up.left.and.arrow.down.right")) { action in
                    if let onHideToolbar = self.onHideToolbar {
                        onHideToolbar()
                    }
                },
                UIAction(title: "请求桌面网站", image: UIImage(systemName: "desktopcomputer")) { action in
                },
            ]),
            UIMenu(options: .displayInline, children: [
                UIAction(title: "网站设置", image: UIImage(systemName: "gear")) { action in
                },
                UIAction(title: "隐私报告", subtitle: "2个跟踪器已被阻止", image: UIImage(systemName: "shield.lefthalf.filled")) { action in
                },
            ]),
            UIAction(title: "显示顶部地址栏", image: UIImage(systemName: "platter.filled.top.and.arrow.up.iphone")) { action in
            },
        ]
    }

    override func replacingChildren(_ newChildren: [UIMenuElement]) -> UIMenu {
        let menu = UIFontSizeMenu()
        menu.onHideToolbar = onHideToolbar
        return menu
    }
}

class UIWebsiteBarMenu: UIMenu {
    var url: URL?

    override var children: [UIMenuElement] {
        [
            UIAction(title: "拷贝", image: UIImage(systemName: "doc.on.doc"), attributes: url == nil ? [.hidden] : []) { action in
                Modules.copy(self.url?.description)
            },
            UIAction(title: "粘贴并搜索") { action in
            },
            UIAction(title: "语音搜索", image: UIImage(systemName: "mic")) { action in
            },
            UIMenu(options: .displayInline, children: [
                UIMenu(title: "移到标签页组", image: UIImage(systemName: "arrow.up.right.square"), children: [
                    UIAction(title: "新建标签页组", image: UIImage(systemName: "plus.rectangle.on.rectangle")) { action in
                    },
                    UIMenu(options: .displayInline, children: [
                        UIAction(title: "24个标签页", image: UIImage(systemName: "iphone")) { action in
                        },
                    ]),
                ]),
            ])
        ]
    }

    override func replacingChildren(_ newChildren: [UIMenuElement]) -> UIMenu {
        let menu = UIWebsiteBarMenu()
        menu.url = url
        return menu
    }
}

class UIReadingListMenu: UIMenu {
    override var children: [UIMenuElement] {
        [
            UIAction(title: "加入阅读列表", image: UIImage(systemName: "eyeglasses")) { action in
            },
            UIAction(title: "添加书签", image: UIImage(systemName: "book")) { action in
            },
            UIAction(title: "为23个标签页添加书签", image: UIImage(systemName: "book")) { action in
            },
        ]
    }
}

class UIStackMenu: UIMenu {
    override var children: [UIMenuElement] {
        [
            UIMenu(options: .displayInline, children: [
                UIAction(title: "新建标签页", image: UIImage(systemName: "plus.square.on.square")) { action in
                },
                UIAction(title: "新建无痕标签页", image: UIImage(systemName: "plus.square.fill.on.square.fill")) { action in
                },
                UIMenu(title: "移到标签页组", image: UIImage(systemName: "arrow.up.forward.square"), children: [
                    UIMenu(options: .displayInline, children: [
                        UIAction(title: "新建标签页组", image: UIImage(systemName: "plus.rectangle.on.rectangle")) { action in
                        }
                    ]),
                    UIAction(title: "23个标签页", image: UIImage(systemName: "iphone"), state: .on) { action in
                    },
                ]),
                UIAction(title: "关闭此标签页", image: UIImage(systemName: "xmark"), attributes: .destructive) { action in
                },
                UIAction(title: "关闭所有23个标签页", image: UIImage(systemName: "xmark"), attributes: .destructive) { action in
                },
            ]),
            UIMenu(options: .displayInline, children: [
                UIAction(title: "23个标签页", image: UIImage(systemName: "iphone"), state: .on) { action in
                },
                UIAction(title: "无痕浏览", image: UIImage(systemName: "hand.raised")) { action in
                },
            ]),
        ]
    }
}